package com.wxsoft.business.sys.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wxsoft.business.common.controller.BaseController;
import com.wxsoft.business.model.easyui.DataGrid;
import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.sys.pojo.SysDict;
import com.wxsoft.business.sys.service.ISysDictService;
import com.wxsoft.util.JsonUtil;

@Controller
@RequestMapping("/sysDict")
public class SysDictController extends BaseController{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private ISysDictService service;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "sys/sysDictlist";
	}
	
	@RequestMapping("/datagrid")
	public void list(HttpServletRequest request,PageHelper page, SysDict sysDict,HttpServletResponse response) throws IOException{
		
		DataGrid dg = new DataGrid();
		dg.setTotal(service.findCount(sysDict));
		List<SysDict> sysDictList = service.findAll(page, sysDict);
		dg.setRows(sysDictList);
		JsonUtil.toResponse(response, dg);

	}
	
//	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void add(HttpServletRequest request,HttpServletResponse response, SysDict sysDict) {
		try {
			String r = service.add(sysDict);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public void modify(HttpServletRequest request,HttpServletResponse response, SysDict sysDict) {
		try {
			String r = service.modify(sysDict);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request,HttpServletResponse response, SysDict sysDict) {
		try {
			String r = service.delete(sysDict);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
	/**
	 * 根据类型返回字段选项
	 * @param page
	 * @param keyvalue
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/kvByType")
	public void findByType(String pid, HttpServletResponse response) throws IOException{
		logger.info("pid: "+pid);
		List<SysDict> keyvalueList = service.findByType(pid);
		JsonUtil.toResponseNOPage(response, keyvalueList);
	}
	
}
